const tradeService = require('../services/trade');

// 获取交易记录
exports.getTrades = async (req, res) => {
  try {
    const trades = await tradeService.getTrades(req.query);
    res.json(trades);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 获取当前持仓
exports.getPositions = async (req, res) => {
  try {
    const positions = await tradeService.getPositions();
    res.json(positions);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 手动下单
exports.createOrder = async (req, res) => {
  try {
    const order = await tradeService.createOrder(req.body);
    res.status(201).json(order);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 取消订单
exports.cancelOrder = async (req, res) => {
  try {
    const result = await tradeService.cancelOrder(req.params.id);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// 获取订单详情
exports.getOrderById = async (req, res) => {
  try {
    const order = await tradeService.getOrderById(req.params.id);
    if (!order) {
      return res.status(404).json({ error: '订单不存在' });
    }
    res.json(order);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};